<template>
  <header class="hc-header">
    <img class="icon" :src="logoImg" />
    <div class="header-content">Master</div>

    <div class="user">
      <el-dropdown>
        <div class="el-dropdown-link">
          admin
          <el-icon><ArrowDown /></el-icon>
        </div>

        <template #dropdown>
          <el-dropdown-menu @command="1">
            <el-dropdown-item>修改密码</el-dropdown-item>
            <el-dropdown-item>切换用户</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>

      <div class="time">
        <p>{{ dateStr }}</p>
        <p>{{ timeStr }}</p>
      </div>
    </div>
  </header>
</template>

<script lang="ts" setup>
import { ArrowDown } from "@element-plus/icons-vue";
import { useAppStore } from "/@/stores/appStore";
import logoImg from "/@/assets/img/logo-text.png";
const store = useAppStore();
const { setGlobalConfig } = store;
const { globalConfig } = storeToRefs(store);

const dateStr = ref("");
const timeStr = ref("");
let timer = 0;

const changeTime = () => {
  dateStr.value = new Date().toLocaleDateString();
  timeStr.value = new Date().toLocaleTimeString();
};

const toggleTheme = () => {
  setGlobalConfig({
    theme: globalConfig.value.theme == "light" ? "dark" : "light",
  });
};

onMounted(() => {
  changeTime();
  timer = window.setInterval(changeTime, 1000);
});

onBeforeUnmount(() => {
  timer && window.clearInterval(timer);
});
</script>
<style lang="scss" scoped>
.hc-header {
  height: var(--hc-header-height);
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
  color: white;
  display: flex;
  align-items: center;
  background-image: url("/@/assets/img/header-bg.png");
  background-size: cover;

  .icon {
    margin-top: 0.714286rem /* 10/14 */;
    cursor: pointer;
    height: 80%;
    display: flex;
    align-items: center;
  }

  .header-content {
    height: 50%;
    flex: 1;
    display: flex;
    align-items: center;
    font-size: 1.857143rem /* 26/14 */;
    color: #a5eae6;
    border-left: 2px solid #a5eae6;
    margin-left: 0.714286rem /* 10/14 */;
    padding-left: 0.714286rem /* 10/14 */;
  }

  .user {
    height: 100%;
    display: flex;
    align-items: center;
    .el-dropdown-link {
      color: white;
      cursor: pointer;
      display: flex;
      align-items: center;
      &:focus-visible {
        outline: none;
      }
    }
    .time {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: center;
      padding: 0 1.428571rem /* 20/14 */;
    }
  }
}
</style>
